//
//  MemoRootViewController.m
//  MokoAPP
//
//  Created by 현식 강 on 11. 8. 17..
//  Copyright 2011 아이온. All rights reserved.
//

#import "MemoRootViewController.h"
#import "MemoListTableViewController.h"
#import "MokoAppDelegate.h"

@implementation MemoRootViewController
-(void)memoNotification:(NSNotification*)noti {
    GET_APPDELEGATE;
    if( [delegate.configVO.showMemoNoti boolValue] == YES ) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"쪽지가 도착하였습니다." message:@"쪽지를 확인하시겠습니까?" delegate:self cancelButtonTitle:@"취소" otherButtonTitles:@"확인", @"알림해제", nil];
        alert.tag = 1;
        [alert show];
        [alert release];
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if( alertView.tag == 1 ) {
        if( buttonIndex == 1 ) {
            [self firstList];
            self.tabBarController.selectedIndex = 5;
        } else if( buttonIndex == 2 ) {
            GET_APPDELEGATE;
            delegate.configVO.showMemoNoti = [NSNumber numberWithBool:NO];
        }
    }
}
-(void) loadView {
    NSArray* buttonNames = [NSArray arrayWithObjects:@"받은쪽지함", @"보낸쪽지함", nil];
    UISegmentedControl* segControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
    [segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
    //[segControl setSelectedSegmentIndex:0];
    [segControl addTarget:self action:@selector(switchViewController:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segControl;
    [segControl release];
    inbox = [[MemoListTableViewController alloc] initWithNibName:@"MemoListTableViewController" bundle:[NSBundle mainBundle]];
    inbox.view.tag = 0;
    inbox.parent = self;
    GET_APPDELEGATE;
    inbox.addr = [NSString stringWithFormat:@"%@/zboard4/member_memo.php", delegate.configVO.siteHostAddress];
    sentbox = [[MemoListTableViewController alloc] initWithNibName:@"MemoListTableViewController" bundle:[NSBundle mainBundle]];
    sentbox.parent = self;
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    sentbox.addr = [NSString stringWithFormat:@"%@/zboard4/member_memo2.php", delegate.configVO.siteHostAddress];
    self.view = view;
    [view release];
    [self.view addSubview:sentbox.view];
    [self.view addSubview:inbox.view];
    sentbox.view.tag = 1;
    memoViewControllers = [[NSArray arrayWithObjects:inbox, sentbox, nil] retain];
}
-(void)switchViewController:(id)sender {
    MemoListTableViewController* controller = ((MemoListTableViewController*)[memoViewControllers objectAtIndex:[sender selectedSegmentIndex]]);
    [self.view bringSubviewToFront:controller.tableView];
    [controller firstList];
}
- (void)dealloc
{
    [sentbox release];
    [inbox release];
    [memoViewControllers release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

-(void)backStep {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
        UIImage* image = [UIImage imageNamed:@"back_left_button.png"];
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:image forState:UIControlStateNormal];
        [button addTarget:self action:@selector(backStep) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
        UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationItem.hidesBackButton = YES;
        self.navigationItem.leftBarButtonItem = item;
        [item release];
    }
    
    [(UISegmentedControl*)self.navigationItem.titleView setSelectedSegmentIndex:0];
    
    UIImage* image = [UIImage imageNamed:@"delete.png"];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(deleteItem) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 25, 25);
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = item;
    [item release];
    for( MemoListTableViewController* controller in memoViewControllers )
        controller.tableView.editing = NO;
}
-(void)deleteItem {
    UIImage* image = [UIImage imageNamed:@"done.png"];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 25, 25);
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = item;
    [item release];
    for( MemoListTableViewController* controller in memoViewControllers )
        [controller.tableView setEditing:YES animated:YES];
}
-(void)done {
    UIImage* image = [UIImage imageNamed:@"delete.png"];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(deleteItem) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 25, 25);
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = item;
    [item release];
    for( MemoListTableViewController* controller in memoViewControllers )
        [controller.tableView setEditing:NO animated:YES];
}

-(void)firstList {
    int selectedIndex = ((UISegmentedControl*)self.navigationItem.titleView).selectedSegmentIndex;
    MemoListTableViewController* controller = ((MemoListTableViewController*)[memoViewControllers objectAtIndex:selectedIndex]);
    [controller firstList];
    
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
